﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace TRicheControl.WinformControl
{
    [ToolboxBitmap(typeof(RadioButton))]
    public partial class RicheRadioButton : Control
    {
        private bool mIsCheck = false;
        public bool IsCheck 
        { 
            get
            {
                return mIsCheck;
            }
            set
            {
                if (mIsCheck == false && value == true)
                {
                    ResetOtherRadioCheckState();
                }
                mIsCheck = value;
                this.Invalidate(mRadioButtonRect);
            }
        }
        private Rectangle mRadioButtonRect;
        private Rectangle mRadioTextRect;
        private Size mRadioButtonSize = new System.Drawing.Size(15, 15);
        public Size RadioButtonSize
        {
            get
            {
                return mRadioButtonSize;
            }
            set
            {
                mRadioButtonSize = value;
            }
        }
        private Padding mTextPadding = new Padding(5);
        public Padding TextPadding
        {
            get
            {
                return mTextPadding;
            }
            set 
            {
                mTextPadding = value;
            }
        }
        private Graphics mGraphics;
        public Graphics Graphics
        {
            get
            {
                if (base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null)
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }
        private int mGroupID;
        public int GroupID
        {
            get
            {
                return mGroupID;
            }
            set
            {
                mGroupID = value;
            }
        }

        public RicheRadioButton()
        {
            InitializeComponent();
            ResetRadioButtonRect();
        }

        protected override void OnResize(EventArgs e)
        {
            ResetRadioButtonRect();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawRadioButton(e.Graphics);
        }

        private void ResetRadioButtonRect()
        {
            Point location = new Point(0, (this.Height - mRadioButtonSize.Height) / 2);
            Point strLocation = new Point(mRadioButtonSize.Width + mTextPadding.Left, ((this.Height - this.FontHeight) / 2) + 1);
            mRadioButtonRect = new Rectangle(location, mRadioButtonSize);
            mRadioTextRect = new Rectangle(strLocation, new Size(this.Width - mRadioButtonSize.Width + mTextPadding.Left, this.Height));
        }

        private void ResetOtherRadioCheckState()
        {
            if (this.Parent == null)
                return;
            foreach (Control control in this.Parent.Controls)
            {
                if (!(control is RicheRadioButton))
                    continue;
                RicheRadioButton radioButton = (control as RicheRadioButton);
                if (radioButton.GroupID == this.GroupID)
                {
                    radioButton.IsCheck = false;
                }
            }
        }

        private void DrawRadioButton(Graphics g)
        {
            if (mIsCheck == true)
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawEanbelCheckRadio(g, mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawUnEanbelCheckRadio(g, mRadioButtonRect);
                }
            }
            else
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawEanbelUnCheckRadio(g, mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawUnEanbelUnCheckRadio(g, mRadioButtonRect);
                }
            }
            GraphicsStyle.Styles.RadioButtonStyle.DrawString(this.Text, this.Font, this.ForeColor, g, mRadioTextRect);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.Graphics.FillRectangle(new SolidBrush(Color.Transparent), this.mRadioButtonRect);
            if (this.IsCheck == true)
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseEnterEanbelCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseEnterUnEanbelCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
            }
            else
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseEnterEanbelUnCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseEnterUnEanbelUnCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
            }
            base.OnMouseEnter(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.Graphics.FillRectangle(new SolidBrush(Color.Transparent), this.mRadioButtonRect);
            if (this.IsCheck == true)
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseDownEanbelCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseDownUnEanbelCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
            }
            else
            {
                if (this.Enabled)
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseDownEanbelUnCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
                else
                {
                    GraphicsStyle.Styles.RadioButtonStyle.DrawMouseDownUnEanbelUnCheckRadio(this.Graphics, this.mRadioButtonRect);
                }
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            this.IsCheck = true;
            base.OnMouseClick(e);
        }
    }
}
